package im.yisme.view.ui.animation;

import java.util.ArrayList;


import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import im.yisme.R;
import im.yisme.view.ui.animation.pojo.ClockPOJO;

public class Clock extends Thread{

	private ClockPOJO clock;
	private ArrayList<ClockPOJO> list;
	private ArrayList<Clock> threadList;
	private long bestTime;
	private boolean pause;
	public static final double DURATION=1500;
	private static final int ACTIVE_TIME_AFTER=500;
	private static final int ANIMATION_UPDATE_INTERVAL=20;
	private static final int PAUSE_POLLING_INTERVAL=500;
	private static final int FINE_TUNING = 200;
	private Bitmap sector1,sector2,substrate;
	
	public Clock(long bestTime,PointF center, int r ,ArrayList<ClockPOJO> clockArrayList,ArrayList<Clock> clockThreadArrayList,Resources res){
		
		this.bestTime=bestTime;
		sector1 = BitmapFactory.decodeResource(res, R.drawable.game_clock_1);
		sector2 = BitmapFactory.decodeResource(res, R.drawable.game_clock_2);
		substrate = BitmapFactory.decodeResource(res, R.drawable.game_clock_1);
		clock=new ClockPOJO(sector1,
				            new PointF(center.x-r,center.y-r));
		
		list=clockArrayList;
		threadList=clockThreadArrayList;
		
		list.add(clock);
		threadList.add(this);
		
		pause=false;
	}
	public void run(){
		
		int degree;//角度
		
		try {
		 
		while(System.currentTimeMillis() < bestTime+ACTIVE_TIME_AFTER){
	
			while(pause) Thread.sleep(PAUSE_POLLING_INTERVAL);
			
			degree=(int) (360*((DURATION-(bestTime-System.currentTimeMillis() - FINE_TUNING))/DURATION));
			
			
			Bitmap sectorTemp,substrateTemp;
			if(degree > 360){
				clock.setSectorAlpha(255-(degree-360)*2);
				degree = 360;
				sectorTemp = sector2;				
				substrateTemp = null;
			}
			else {
				clock.setSectorAlpha(255);
				sectorTemp = getSectorClip(sector1, sector1.getWidth()/2, sector1.getHeight()/2, sector1.getWidth()/2, 0, degree);
				substrateTemp = getSectorClip(substrate, substrate.getWidth()/2, substrate.getHeight()/2, substrate.getWidth()/2, 0, degree+10);

			}

			clock.setSector(sectorTemp);
			clock.setSubstrate(substrateTemp);
					

			Thread.sleep(ANIMATION_UPDATE_INTERVAL);
		}
		
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		list.remove(clock);
		threadList.remove(this);
		sector1.recycle();
		sector2.recycle();
	}
	public void pauseRunning(){
		pause=true;
	}
	public void resumeRunning(long time){
		pause=false;
		bestTime=bestTime+time;
	}
	private Bitmap getSectorClip(Bitmap input,float center_X,float center_Y,float r,float startAngle,float sweepAngle){
		
		int width = input.getWidth();
        
        float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;
        
        top = 0;
        bottom = width;
        left = 0;
        right = width;
        dst_left = 0;
        dst_top = 0;
        dst_right = width;
        dst_bottom = width;
        
        Bitmap output = Bitmap.createBitmap(width, width, Config.ARGB_8888);
        
		Canvas canvas = new Canvas(output);
		
		
		final int color = 0xff000000;
        final Paint paint = new Paint();
        final Rect src = new Rect((int)left, (int)top, (int)right, (int)bottom);
        final Rect dst = new Rect((int)dst_left, (int)dst_top, (int)dst_right, (int)dst_bottom);
        
		Path path = new Path();
		
		path.moveTo(center_X, center_Y);  //圆心 

        path.lineTo((float)(center_X+r*Math.sin(startAngle* Math.PI / 180)),   //起始点角度在圆上对应的横坐标 

        			(float)(center_Y-r*Math.cos(startAngle* Math.PI / 180)));    //起始点角度在圆上对应的纵坐标 

        path.lineTo((float)(center_X+r*Math.sin(sweepAngle* Math.PI / 180)),  //终点角度在圆上对应的横坐标 

                	(float)(center_Y-r*Math.cos(sweepAngle* Math.PI / 180)));   //终点点角度在圆上对应的纵坐标 

        path.close(); 
        
        RectF rectF = new RectF(center_X-r,center_Y-r,center_X+r,center_Y+r);
		path.addArc(rectF, startAngle-90, sweepAngle);

		
		paint.setAntiAlias(true);    
		paint.setStyle(Paint.Style.FILL);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawPath(path, paint);
        
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(input, src, dst, paint);
		
		return output;
	}
}
